USE [SistemaDeLeiloes]
GO
/****** Object:  StoredProcedure [dbo].[sp_UpdEncerraLeilao]    Script Date: 05/27/2014 14:37:44 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[sp_UpdEncerraLeilao]
	@idLeilao INT,
	@senha VARCHAR(255)
AS
BEGIN
	SET NOCOUNT ON;

	IF EXISTS(SELECT 1 FROM leiloes WHERE id = @idLeilao AND senha = @senha)
	BEGIN
		UPDATE leiloes SET status = 0 WHERE id = @idLeilao
		
		SELECT	la.idLeilao AS idLeilao,
				p.nome AS nomeProduto,
				la.valor AS valor,
				u.email AS email
		FROM lances la
			INNER JOIN usuario u ON la.idUsuario = u.id
			INNER JOIN leiloes le ON le.id = la.idLeilao
			INNER JOIN produtos p ON p.id = le.idProduto
		WHERE	la.idLeilao = @idLeilao
	END
	ELSE
	BEGIN
		RAISERROR('Erro ao encerrar',16,1);  	
	END

END

